<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>gitee 一键部署</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        .hint {
            color: #666;
            font-size: 0.9em;
            margin-top: 5px;
        }
        .path-info {
            background-color: #e9f7fe;
            padding: 10px 15px;
            border-radius: 4px;
            margin: 15px 0;
            font-size: 0.95em;
            color: #0277bd;
            border-left: 3px solid #03a9f4;
        }
        .btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
            width: 100%;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #2980b9;
        }
        .btn:disabled {
            background-color: #95a5a6;
            cursor: not-allowed;
        }
        .status {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .nginx-status {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
        .warning {
            background-color: #fff3cd;
            color: #856404;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        pre {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
            margin-top: 20px;
            border: 1px solid #ddd;
            white-space: pre-wrap;
        }
        .version {
            text-align: center;
            margin-top: 20px;
            font-size: 12px;
            color: #666;
        }
        .nginx-config {
            font-family: monospace;
            white-space: pre;
            overflow-x: auto;
            background: #f1f1f1;
            padding: 10px;
            border-radius: 4px;
            border-left: 3px solid #3498db;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>gitee 一键部署工具</h1>
        
        <div class="form-group">
            <label for="domain">域名:</label>
            <input type="text" id="domain" placeholder="例如: pdf.example.com" required>
            <div class="hint">请输入完整域名，将会自动配置Nginx</div>
        </div>
        
        <div class="form-group">
            <label for="gitrepo">Gitee 项目名:</label>
            <input type="text" id="gitrepo" placeholder="例如: username/repo-name 或 https://gitee.com/username/repo-name" required>
            <div class="hint">可以输入项目路径或完整URL，如有https://gitee.com前缀会自动处理</div>
        </div>
        
        <div class="path-info">
            注意: 项目将自动部署到 <strong>/projects/仓库名</strong> 目录下
        </div>
        
        <div class="form-group">
            <label for="command">运行命令名:</label>
            <input type="text" id="command" placeholder="例如: docker-compose up -d" required>
            <div class="hint">部署后要执行的命令</div>
        </div>
        
        <button class="btn" id="deployBtn">一键部署</button>
        
        <div class="status" id="status"></div>
        <div id="nginxStatus" style="display:none;"></div>
        <pre id="output"></pre>
        
        <div class="version">gitee 部署工具 v1.2 - FastAPI版</div>
    </div>

    <script>
        document.getElementById('deployBtn').addEventListener('click', function() {
            const domain = document.getElementById('domain').value.trim();
            const gitrepo = document.getElementById('gitrepo').value.trim();
            const command = document.getElementById('command').value.trim();
            
            // 验证输入
            if (!domain || !gitrepo || !command) {
                showStatus('请填写所有字段', false);
                return;
            }
            
            // 显示正在部署状态
            showStatus('正在部署，请稍候...', true);
            document.getElementById('deployBtn').disabled = true;
            document.getElementById('nginxStatus').style.display = 'none';
            
            // 构建要执行的命令
            fetch('/deploy', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    domain: domain,
                    gitrepo: gitrepo,
                    command: command
                })
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态: ${response.status}`);
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    let successMsg = '部署成功!';
                    
                    // 显示项目路径
                    if (data.project_path) {
                        successMsg += ` 项目已部署到 ${data.project_path}`;
                    }
                    
                    showStatus(successMsg, true);
                    
                    // 显示Nginx配置状态
                    if (data.nginx_success) {
                        showNginxStatus(`Nginx已成功配置！域名 ${domain} 已设置为指向您的应用。`, 'success');
                    } else if (data.nginx_message) {
                        showNginxStatus(`Nginx配置警告: ${data.nginx_message}`, 'warning');
                    }
                    
                    document.getElementById('output').textContent = data.output;
                } else {
                    showStatus('部署失败: ' + (data.error || '未知错误'), false);
                    
                    // 显示Nginx配置状态（如果有）
                    if (data.nginx_message) {
                        showNginxStatus(`Nginx配置: ${data.nginx_message}`, data.nginx_success ? 'success' : 'error');
                    }
                    
                    document.getElementById('output').textContent = data.output || '';
                }
            })
            .catch(error => {
                showStatus('请求错误: ' + error.message, false);
            })
            .finally(() => {
                document.getElementById('deployBtn').disabled = false;
            });
        });
        
        function showStatus(message, isSuccess) {
            const statusEl = document.getElementById('status');
            statusEl.textContent = message;
            statusEl.style.display = 'block';
            
            if (isSuccess) {
                statusEl.className = 'status success';
            } else {
                statusEl.className = 'status error';
            }
            
            // 滚动到状态消息
            statusEl.scrollIntoView({ behavior: 'smooth' });
        }
        
        function showNginxStatus(message, type) {
            const nginxStatusEl = document.getElementById('nginxStatus');
            nginxStatusEl.textContent = message;
            nginxStatusEl.style.display = 'block';
            
            if (type === 'success') {
                nginxStatusEl.className = 'nginx-status success';
            } else if (type === 'warning') {
                nginxStatusEl.className = 'nginx-status warning';
            } else {
                nginxStatusEl.className = 'nginx-status error';
            }
        }
    </script>
</body>
</html>
